<?php
if( !defined('__ROOT__') ) die('Access denied');
class Controller extends Core{
	
	private static $ins;
	private static $self;
	protected $model = null;

	public function __construct(){
	}

	public static function load($controller="index"){
		$controller = empty($controller) ? "index.index" : $controller;
		if( is_null(self::$ins) ){
			$c = C($controller);
			if( empty($c) ){
				die('Controller loading error');
			}
			self::$ins = $c;
		}	
		$arr = explode('.',$controller);
		$model = count($arr)==1 ? $controller : $arr[1];
		self::$ins->init($model);
		return self::$ins;
	}

	private function init($model){
		/*$this->smarty = new Tpl();
		$this->assign('__URL__',__URL__);
		$this->assign('VAR_URL',VAR_URL);
		$this->assign('__LOCATION__',__URL__.'index.php?'.G('MODULE').'/'.G('CONTROLLER').'/');
		$this->model = load_model($model);*/
		return;
	}

	public function run($action="index"){
		$action = empty($action) ? "index" : $action;
		if( method_exists($this,$action) ){
			$this->$action();
		}else{
			$this->error('Call to undefined action');
		}
		return;
	}

	public function view($tpl='', $data=''){
		$tpl = !empty($tpl) ? $tpl : MODULE.'/'.CONTROLLER.'_'.ACTION;
		$view = new View();
		$view->display($tpl, $data);
	}

}
